import Gobj from "./Gobj";
import GUniverse from "./GUniverse";
import VEnemy from "./VEnemy";
import Gstatic from "./Gstatic";
import GameWx from "../GameWx";

export default class GEnemy extends Gobj{

    constructor(){
        super();
        this.type = Gobj.GEnemy;
        this.speed  = Gstatic.speed;
        this.ylen   = Gstatic.ylen;
        this.xlen   = Gstatic.xlen;
        this.xylen  = this.ylen + this.xlen;
        this.maxlen = this.ylen + this.xlen + this.ylen;        
    }

    ylen   = 0;
    xlen   = 0;
    xylen  = 0;
    maxlen = 0;

    get die(){
        return this.v0 == 1;
    }

    set die(b:boolean){
        this.v0 = b?1:0;
    }

    copy():Gobj{
        var o = new GEnemy();
        o.syncTime(this);
        o.id = this.id;
        return o;
    }

    timeline(sf:number , lf:number , fms:number , keyf:boolean , u:GUniverse, ac:any){
        if(fms>this.st){
            this.z+= this.speed;        
            if(this.z>= this.maxlen){
                this.z = this.maxlen;
                //console.log(this.x , this.y)            
                if(!this.die){
                    this.die = true;
                    var player = u.gm.getPlayer(this.pid);
                    player.monsterMap.removeobj(this.key);                    
                    if(player.hp>0){
                        player.hp -= 1;                                                
                        // if(player.hp<=0 && this.uid==1){//如果是第一宇宙则
                        //     //u.gm.gameover = true;
                        //     //游戏结束 ,playerid ,lf
                        //     //GameWx.I.uploadFrame("9,"+u.gm.getPlayer(1).hp+","+u.gm.getPlayer(2).hp+","+lf);
                        // }
                    }
                    //console.log(" e_"+this.uid+"_"+this.id + " 死于第 "+lf+" 帧");
                }
            }
        }
        GEnemy.z2xy_GEnemy(this);
    }
    
    static z2xy_GEnemy(e:GEnemy){
        
        if(e.z<=e.ylen){
            e.x = 0;
            e.y = e.z;
            return e;
        }

        if(e.z<=e.xylen){
            e.x = e.z - e.ylen;
            e.y = e.ylen;
            return e;
        }

        e.x = e.xlen;
        e.y = e.ylen - (e.z - e.xylen);
        return e;
    }

}